function fig = fm_stat(varargin)
% FM_STAT create GUI for power flow reports
%
% HDL = FM_STAT(VARARGIN)
%
%Author:    Federico Milano
%Date:      11-Nov-2002
%Update:    24-Aug-2003
%Version:   2.0.0
%
%E-mail:    federico.milano@ucd.ie
%Web-site:  faraday1.ucd.ie/psat.html
%
% Copyright (C) 2002-2019 Federico Milano

global Bus DAE Varname Settings Fig Path OPF Theme
global Oxl File

abus = DAE.y(Bus.a);
vbus = DAE.y(Bus.v);

if nargin && ischar(varargin{1})
  switch varargin{1}

   case 'report'

    if OPF.init == 1 || OPF.init == 2
      fm_opfrep
    else
      fm_report
    end

   case 'toplist'

    value = get(gcbo,'Value');
    set(gcbo,'Value',value(end))
    set(get(gcf,'UserData'), ...
        'Value',value(end), ...
        'ListboxTop',get(gcbo,'ListboxTop'));

   case 'checkabs'

    hdl = findobj(Fig.stat,'Tag','CheckABS');
    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      Settings.absvalues = 'off';
      set(hdl,'Value',0);
     case 'off'
      set(gcbo,'Checked','on')
      Settings.absvalues = 'on';
      set(hdl,'Value',1);
    end

   case 'report_type'

    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      Settings.report = 0;
     case 'off'
      set(gcbo,'Checked','on')
      Settings.report = 1;
    end

   case 'violation'

    hdl = findobj(Fig.stat,'Tag','CheckVIOL');
    switch get(gcbo,'Checked')
     case 'on'
      set(gcbo,'Checked','off')
      Settings.violations = 'off';
      set(hdl,'Value',0);
     case 'off'
      set(gcbo,'Checked','on')
      Settings.violations = 'on';
      set(hdl,'Value',1);
    end

   case 'abscheck'

    hdl = findobj(Fig.stat,'Tag','absvalues');
    switch get(gcbo,'Value')
     case 1
      Settings.absvalues = 'on';
      set(hdl,'Checked','on');
     case 0
      Settings.absvalues = 'off';
      set(hdl,'Checked','off');
    end

   case 'violcheck'

    hdl = findobj(Fig.stat,'Tag','violations');
    switch get(gcbo,'Value')
     case 1
      Settings.violations = 'on';
      set(hdl,'Checked','on');
     case 0
      Settings.violations = 'off';
      set(hdl,'Checked','off');
    end

   case 'sort'

    hdl = findobj(gcf,'Tag','PushSort');
    maxn = 150;
    switch get(hdl,'UserData')
     case 'az'
      [a,ordbus] = sortbus_bus(Bus,maxn);
      [a,ordbus] = sort(getidx_bus(Bus,ordbus));
      h = get(gcf,'UserData');
      for i = 1:length(h)
        String = get(h(i),'String');
        set(h(i),'String',String(ordbus))
      end
      set(hdl,'UserData','1n','CData',fm_mat('stat_sort12'))
     case '1n'
      [a,ordbus] = sortbus_bus(Bus,maxn);
      h = get(gcf,'UserData');
      for i = 1:length(h)
        String = get(h(i),'String');
        set(h(i),'String',String(ordbus))
      end
      set(hdl,'UserData','az','CData',fm_mat('stat_sortaz'))
    end

   case 'kvpu'

    hdl = findobj(gcf,'Tag','PushVoltage');
    switch get(hdl,'UserData')
     case 'pu'
      set(findobj(Fig.stat,'Tag','ListboxV'),'String',setvar(vbus.*getkv_bus(Bus,0,0)));
      set(hdl,'UserData','kv','CData',fm_mat('stat_kv'))
     case 'kv'
      set(findobj(Fig.stat,'Tag','ListboxV'),'String',setvar(vbus));
      set(hdl,'UserData','pu','CData',fm_mat('stat_pu'))
    end

   case 'plotv'

    hdl = findobj(gcf,'Tag','PushVoltage');
    figure
    switch get(hdl,'UserData')
     case 'pu'
      bar(vbus)
      ylabel('V [p.u.]')
     case 'kv'
      bar(vbus.*getkv_bus(Bus,0,0))
      ylabel('V [kV]')
    end
    title('Voltage Magnitude Profile')
    xlabel('Bus #')

   case 'raddeg'

    hdl = findobj(gcf,'Tag','PushAngle');
    switch get(hdl,'UserData')
     case 'deg'
      set(findobj(Fig.stat,'Tag','ListboxAng'),'String',setvar(abus));
      set(hdl,'UserData','rad','CData',fm_mat('stat_rad'))
     case 'rad'
      set(findobj(Fig.stat,'Tag','ListboxAng'),'String',setvar(abus*180/pi));
      set(hdl,'UserData','deg','CData',fm_mat('stat_deg'))
    end

   case 'plota'

    hdl = findobj(gcf,'Tag','PushAngle');
    figure
    switch get(hdl,'UserData')
     case 'rad'
      bar(abus)
      ylabel('\theta [rad]')
     case 'deg'
      bar(abus*180/pi)
      ylabel('\theta [deg]')
    end
    title('Voltage Phase Profile')
    xlabel('Bus #')

   case 'realpower'

    hdl1 = findobj(gcf,'Tag','PushPGL');
    switch get(hdl1,'UserData')
    case 'I', set(hdl1,'UserData','L')
    case 'G', set(hdl1,'UserData','I')
    case 'L', set(hdl1,'UserData','G')
    end
    hdl2 = findobj(gcf,'Tag','PushP');
    switch get(hdl2,'UserData')
     case 'mw'
      set(hdl2,'UserData','pu','CData',fm_mat('stat_pu'))
     case 'pu'
      set(hdl2,'UserData','mw','CData',fm_mat('stat_mw'))
    end
    fm_stat pgl

   case 'pgl'

    hdl1 = findobj(gcf,'Tag','PushPGL');
    hdl2 = findobj(gcf,'Tag','PushP');
    switch get(hdl2,'UserData')
     case 'pu', mva = 1;
     case 'mw', mva = Settings.mva;
    end
    switch get(hdl1,'UserData')
     case 'I'
      pgl = Bus.Pg;
      set(hdl1,'UserData','G','CData',fm_mat('stat_pqg'))
     case 'G'
      pgl = Bus.Pl;
      set(hdl1,'UserData','L','CData',fm_mat('stat_pql'))
     case 'L'
      pgl = Bus.Pg - Bus.Pl;
      set(hdl1,'UserData','I','CData',fm_mat('stat_pqi'))
    end
    set(findobj(Fig.stat,'Tag','ListboxP'),'String',setvar(pgl*mva));

   case 'qgl'

    hdl1 = findobj(gcf,'Tag','PushQGL');
    hdl2 = findobj(gcf,'Tag','PushQ');
    switch get(hdl2,'UserData')
     case 'pu', mva = 1;
     case 'mvar', mva = Settings.mva;
    end
    switch get(hdl1,'UserData')
     case 'I'
      qgl = Bus.Qg;
      set(hdl1,'UserData','G','CData',fm_mat('stat_pqg'))
     case 'G'
      qgl = Bus.Ql;
      set(hdl1,'UserData','L','CData',fm_mat('stat_pql'))
     case 'L'
      qgl = Bus.Qg - Bus.Ql;
      set(hdl1,'UserData','I','CData',fm_mat('stat_pqi'))
    end
    set(findobj(Fig.stat,'Tag','ListboxQ'),'String',setvar(qgl*mva));

   case 'plotp'

    switch get(findobj(Fig.stat,'Tag','PushP'),'UserData')
     case 'pu', mva = 1;            unit = 'p.u.';
     case 'mw', mva = Settings.mva; unit = 'MW';
    end
    switch get(findobj(Fig.stat,'Tag','PushPGL'),'UserData')
     case 'I'
      pgl = Bus.Pg - Bus.Pl;
      tag = 'P_G - P_L';
     case 'G'
      pgl = Bus.Pg;
      tag = 'P_G';
     case 'L'
      pgl = Bus.Pl;
      tag = 'P_L';
    end

    figure
    bar(pgl*mva)
    ylabel([tag,' [',unit,']'])
    title('Real Power Profile')
    xlabel('Bus #')

   case 'reactivepower'

    hdl1 = findobj(gcf,'Tag','PushQGL');
    switch get(hdl1,'UserData')
     case 'I', set(hdl1,'UserData','L')
     case 'G', set(hdl1,'UserData','I')
     case 'L', set(hdl1,'UserData','G')
    end
    hdl2 = findobj(gcf,'Tag','PushQ');
    switch get(hdl2,'UserData')
     case 'mvar'
      set(hdl2,'UserData','pu','CData',fm_mat('stat_pu'))
     case 'pu'
      set(hdl2,'UserData','mvar','CData',fm_mat('stat_mvar'))
    end
    fm_stat qgl

   case 'plotq'

    switch get(findobj(Fig.stat,'Tag','PushQ'),'UserData')
     case 'pu',   mva = 1;            unit = 'p.u.';
     case 'mvar', mva = Settings.mva; unit = 'MVar';
    end
    switch get(findobj(Fig.stat,'Tag','PushQGL'),'UserData')
     case 'I'
      qgl = Bus.Qg-Bus.Ql;
      tag = 'Q_G - Q_L';
     case 'G'
      qgl = Bus.Qg;
      tag = 'Q_G';
     case 'L'
      qgl = Bus.Ql;
      tag = 'Q_L';
    end

    figure
    bar(qgl*mva)
    ylabel([tag,' [',unit,']'])
    title('Reactive Power Profile')
    xlabel('Bus #')

  end
  return
end

% check for data file
if isempty(File.data)
  fm_disp('Set a data file for viewing static report.',2)
  return
end
% check for initial power flow solution
if ~Settings.init
  fm_disp('Solve base case power flow...')
  Settings.show = 0;
  fm_set('lf')
  Settings.show = 1;
  if ~Settings.init, return, end
end

if ishandle(Fig.stat)

  figure(Fig.stat)
  set(findobj(Fig.stat,'Tag','ListboxBus'),'String',setbus);

  if strcmp(get(findobj(Fig.stat,'Tag','PushAngle'),'UserData'),'rad')
    set(findobj(Fig.stat,'Tag','ListboxAng'),'String',setvar(abus));
  else
    set(findobj(Fig.stat,'Tag','ListboxAng'),'String',setvar(abus*180/pi));
  end

  if strcmp(get(findobj(Fig.stat,'Tag','PushVoltage'),'UserData'),'pu')
    set(findobj(Fig.stat,'Tag','ListboxV'),'String',setvar(vbus));
  else
    set(findobj(Fig.stat,'Tag','ListboxV'),'String',setvar(vbus.*getkv_bus(Bus,0,0)));
  end

  switch get(findobj(Fig.stat,'Tag','PushP'),'UserData')
   case 'pu', mva = 1;
   case 'mw', mva = Settings.mva;
  end

  switch get(findobj(Fig.stat,'Tag','PushPGL'),'UserData')
   case 'I', pgl = Bus.Pg-Bus.Pl;
   case 'G', pgl = Bus.Pg;
   case 'L', pgl = Bus.Pl;
  end

  set(findobj(Fig.stat,'Tag','ListboxP'),'String',setvar(pgl*mva));

  switch get(findobj(Fig.stat,'Tag','PushQ'),'UserData')
   case 'pu', mva = 1;
   case 'mvar', mva = Settings.mva;
  end

  switch get(findobj(Fig.stat,'Tag','PushQGL'),'UserData')
   case 'I', qgl = Bus.Qg-Bus.Ql;
   case 'G', qgl = Bus.Qg;
   case 'L', qgl = Bus.Ql;
  end

  set(findobj(Fig.stat,'Tag','ListboxQ'),'String',setvar(qgl*mva));

  set(findobj(Fig.stat,'Tag','ListboxState'),'String',setx);

  hdl = findobj(Fig.stat,'Tag','ListboxServ');
  if nargin > 0
    set(hdl,'String',varargin{1});
    setyvars(hdl,1);
  elseif OPF.init
    set(hdl,'String',OPF.report);
  else
    setyvars(hdl,0);
  end

  return

end

if Bus.n > 150,
  fm_disp(['Only the first 150 buses are reported in the Static', ...
      ' Report GUI'])
end
if DAE.n > 100,
  fm_disp(['Only the first 100 state variables are reported in the Static', ...
      ' Report GUI'])
end

h0 = figure('Color',Theme.color01, ...
  'Units', 'normalized', ...
  'Colormap',[], ...
  'CreateFcn','Fig.stat = gcf;', ...
  'DeleteFcn','Fig.stat = -1;', ...
  'FileName','fm_stat', ...
  'MenuBar','none', ...
  'Name','Static Report', ...
  'NumberTitle','off', ...
  'PaperPosition',[18 180 576 432], ...
  'PaperUnits','points', ...
  'Position',sizefig(0.6984,0.6377), ...
  'Resize','on', ...
  'ToolBar','none');

% Menu File
h1 = uimenu('Parent',h0, ...
  'Label','File', ...
  'Tag','MenuFile');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat report', ...
  'Label','Create report', ...
  'Tag','OTV', ...
  'Accelerator','r');
h2 = uimenu('Parent',h1, ...
  'Callback','close(gcf)', ...
  'Label','Exit', ...
  'Tag','NetSett', ...
  'Accelerator','x', ...
  'Separator','on');

% Menu View
h1 = uimenu('Parent',h0, ...
  'Label','View', ...
  'Tag','MenuView');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat plotv', ...
  'Label','Voltage Profile', ...
  'Tag','v_prof', ...
  'Accelerator','v');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat plota', ...
  'Label','Angle Profile', ...
  'Tag','a_prof', ...
  'Accelerator','a');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat plotp', ...
  'Label','Real Power Profile', ...
  'Tag','v_prof', ...
  'Accelerator','p');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat plotq', ...
  'Label','Reactive Power Profile', ...
  'Tag','a_prof', ...
  'Accelerator','q');

% Menu Preferences
h1 = uimenu('Parent',h0, ...
  'Label','Preferences', ...
  'Tag','MenuPref');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat kvpu', ...
  'Label','Switch voltage units', ...
  'Tag','tvopt', ...
  'Accelerator','k');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat raddeg', ...
  'Label','Switch radiant/degree', ...
  'Tag','tvopt', ...
  'Accelerator','d');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat realpower', ...
  'Label','Switch real power units', ...
  'Tag','tvopt', ...
  'Accelerator','w');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat reactivepower', ...
  'Label','Switch reactive power units', ...
  'Tag','tvopt', ...
  'Accelerator','u');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat pgl', ...
  'Label','Switch real power type', ...
  'Tag','tvopt', ...
  'Accelerator','e');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat qgl', ...
  'Label','Switch reactive power type', ...
  'Tag','tvopt', ...
  'Accelerator','f');

h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat checkabs', ...
  'Label','Use absolute values in report files', ...
  'Tag','absvalues', ...
  'Checked',Settings.absvalues, ...
  'Separator','on', ...
  'Accelerator','b');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat violation', ...
  'Label','Include limit violation checks', ...
  'Tag','violations', ...
  'Checked',Settings.violations, ...
  'Accelerator','l');
h2 = uimenu('Parent',h1, ...
  'Callback','fm_stat report_type', ...
  'Label','Embed line flows in bus report', ...
  'Tag','report_type', ...
  'Checked','off', ...
  'Accelerator','1');
if Settings.report, set(h2,'Checked','on'), end
h2 = uimenu('Parent',h1, ...
  'Callback','fm_tviewer', ...
  'Label','Select Text Viewer', ...
  'Tag','tvopt', ...
  'Separator','on', ...
  'Accelerator','t');

h1 = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color02, ...
    'ForegroundColor',Theme.color03, ...
    'Position',[0.040268 0.48392 0.91499 0.475], ...
    'Style','frame', ...
    'Tag','Frame1');
hP = uicontrol( ...
    'Parent',h0, ...
    'Units', 'normalized', ...
    'BackgroundColor',Theme.color03, ...
    'Callback','fm_stat toplist', ...
    'FontName',Theme.font01, ...
    'ForegroundColor',Theme.color10, ...
    'Max',100, ...
    'Position',[0.60235 0.51149 0.14094 0.36547], ...
    'String',setvar(Bus.Pg-Bus.Pl), ...
    'Style','listbox', ...
    'Tag','ListboxP', ...
    'Value',1);
hQ = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','fm_stat toplist', ...
  'FontName',Theme.font01, ...
  'ForegroundColor',Theme.color10, ...
  'Max',100, ...
  'Position',[0.7774 0.51149 0.14094 0.36547], ...
  'String',setvar(Bus.Qg-Bus.Ql), ...
  'Style','listbox', ...
  'Tag','ListboxQ', ...
  'Value',1);
hB = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','fm_stat toplist', ...
  'FontName',Theme.font01, ...
  'ForegroundColor',Theme.color06, ...
  'Max',100, ...
  'Position',[0.077181 0.51149 0.14094 0.36547], ...
  'String',setbus, ...
  'Style','listbox', ...
  'Tag','ListboxBus', ...
  'Value',1);
hV = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','fm_stat toplist', ...
  'FontName',Theme.font01, ...
  'ForegroundColor',Theme.color10, ...
  'Max',100, ...
  'Position',[0.25224 0.51149 0.14094 0.36547], ...
  'String',setvar(vbus), ...
  'Style','listbox', ...
  'Tag','ListboxV', ...
  'Value',1);
hA = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','fm_stat toplist', ...
  'FontName',Theme.font01, ...
  'ForegroundColor',Theme.color10, ...
  'Max',100, ...
  'Position',[0.42729 0.51149 0.14094 0.36547], ...
  'String',setvar(abus), ...
  'Style','listbox', ...
  'Tag','ListboxAng', ...
  'Value',1);
% Static texts
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HorizontalAlignment','left', ...
  'Position',[0.7774 0.89227 0.12975 0.030628], ...
  'String','Q', ...
  'Style','text', ...
  'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HorizontalAlignment','left', ...
  'Position',[0.60235 0.89227 0.12975 0.030628], ...
  'String','P', ...
  'Style','text', ...
  'Tag','StaticText1');
hT = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HitTest','off', ...
  'HorizontalAlignment','left', ...
  'ListboxTop',0, ...
  'Position',[0.42729 0.89227 0.12975 0.030628], ...
  'String','Va', ...
  'Style','text', ...
  'Tag','StaticTextAng');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HorizontalAlignment','left', ...
  'Position',[0.25224 0.89227 0.12975 0.030628], ...
  'String','Vm', ...
  'Style','text', ...
  'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HorizontalAlignment','left', ...
  'Position',[0.077181 0.89227 0.12975 0.030628], ...
  'String','Bus', ...
  'Style','text', ...
  'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.040268 0.049005 0.32662 0.41041], ...
  'Style','frame', ...
  'Tag','Frame2');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HorizontalAlignment','left', ...
  'Position',[0.072707     0.41348     0.12975    0.030628], ...
  'String','State Variables', ...
  'Style','text', ...
  'Tag','StaticText1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'FontName',Theme.font01, ...
  'ForegroundColor',Theme.color10, ...
  'Max',100, ...
  'Position',[0.072707    0.084227     0.26286       0.317], ...
  'String',setx, ...
  'Style','listbox', ...
  'Tag','ListboxState', ...
  'Value',1);
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'ForegroundColor',Theme.color03, ...
  'Position',[0.41051    0.049005     0.32662     0.41041], ...
  'Style','frame', ...
  'Tag','Frame2');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'FontName',Theme.font01, ...
  'ForegroundColor',Theme.color10, ...
  'Max',100, ...
  'Position',[0.44295    0.084227     0.26286       0.317], ...
  'String',' ', ...
  'Style','listbox', ...
  'Tag','ListboxServ', ...
  'Value',1);

if nargin > 0
  set(h1,'String',varargin{1});
  setyvars(h1,1)
elseif ~isempty(OPF.report)
  set(h1,'String',OPF.report);
else
  setyvars(h1,0)
end

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'HorizontalAlignment','left', ...
  'Position',[0.44295     0.41348     0.12975    0.030628], ...
  'String','Other Variables', ...
  'Style','text', ...
  'Tag','StaticText1');

h1 = axes('Parent',h0, ...
  'Box','on', ...
  'CameraUpVector',[0 1 0], ...
  'CameraUpVectorMode','manual', ...
  'Color',Theme.color04, ...
  'ColorOrder',Settings.color, ...
  'Layer','top', ...
  'Position',[0.77964 0.050394 0.17785 0.176378], ...
  'Tag','Axes1', ...
  'XColor',Theme.color03, ...
  'XLim',[0.5 190.5], ...
  'XLimMode','manual', ...
  'XTickLabelMode','manual', ...
  'XTickMode','manual', ...
  'YColor',Theme.color03, ...
  'YDir','reverse', ...
  'YLim',[0.5 115.5], ...
  'YLimMode','manual', ...
  'YTickLabelMode','manual', ...
  'YTickMode','manual', ...
  'ZColor',[0 0 0]);

if Settings.hostver < 8.04
  h2 = image('Parent',h1, ...
             'CData',imread([Path.images,'stat_fig.jpg'],'jpg'), ...
             'Tag','Axes1Image1', ...
             'XData',[1 190], ...
             'YData',[1 115]);
else
  h2 = image('Parent',h1, ...
             'CData',flipud(fliplr(imread([Path.images,'stat_fig.jpg'],'jpg'))), ...
             'Tag','Axes1Image1', ...
             'XData',[1 190], ...
             'YData',[1 115]);
end

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat abscheck', ...
  'Position',[0.77964 0.050394+0.176378+0.03 0.17785 0.05], ...
  'String','Use absolute values', ...
  'Style','checkbox', ...
  'Tag','CheckABS', ...
  'Value',onoff(Settings.absvalues));
%  'Position',[0.77964 0.050394+0.176378+0.01 0.17785 0.05], ...

%h1 = uicontrol('Parent',h0, ...
%  'Units', 'normalized', ...
%  'BackgroundColor',Theme.color02, ...
%  'Callback','fm_stat xxx', ...
%  'Position',[0.77964 0.1108+0.176378-0.005 0.17785 0.05], ...
%  'String','xxx', ...
%  'Style','checkbox', ...
%  'Tag','CheckSHUNT', ...
%  'Value',onoff(xxx));

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat violcheck', ...
  'Position',[0.77964 0.1712+0.176378-0.04 0.17785 0.05], ...
  'String','Check limit violations', ...
  'Style','checkbox', ...
  'Tag','CheckVIOL', ...
  'Value',onoff(Settings.violations));
%  'Position',[0.77964 0.1712+0.176378-0.02 0.17785 0.05], ...

% Push buttons
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color03, ...
  'Callback','fm_stat report', ...
  'FontWeight','bold', ...
  'ForegroundColor',Theme.color09, ...
  'Position',[0.77964     0.39051     0.0839    0.061256], ...
  'String','Report', ...
  'Tag','Pushbutton1');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'BackgroundColor',Theme.color02, ...
  'Callback','close(gcf)', ...
  'Position',[0.8685       0.39051     0.0839    0.061256], ...
  'String','Close', ...
  'Tag','Pushbutton1');

%  'Position',[0.80425     0.39051     0.12864    0.061256], ...
%  'Position',[0.80425       0.317     0.12864    0.061256], ...
if isunix,
  dy = 0.0025;
else
  dy = 0;
end

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_profile'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat plotv', ...
  'Position',[0.34899   0.89686    0.045861    0.030628+dy], ...
  'Tag','Pushbutton3');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_pu'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat kvpu', ...
  'UserData','pu', ...
  'Position',[0.3     0.89686    0.045861    0.030628+dy], ...
  'Tag','PushVoltage');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_profile'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat plota', ...
  'Position',[0.52349     0.89686    0.045861    0.030628+dy], ...
  'Tag','Pushbutton3');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_rad'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat raddeg', ...
  'UserData','rad', ...
  'Position',[0.4745     0.89686    0.045861    0.030628+dy], ...
  'Tag','PushAngle');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_sortaz'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat sort', ...
  'Position',[0.1740     0.89686    0.045861    0.030628+dy], ...
  'UserData','az', ...
  'Tag','PushSort');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_profile'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat plotp', ...
  'Position',[0.6974 0.89686 0.045861 0.030628+dy], ...
  'Tag','Pushbutton3');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_pu'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat realpower', ...
  'UserData','pu', ...
  'Position',[0.6484     0.89686    0.045861    0.030628+dy], ...
  'Tag','PushP');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_pqi'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat pgl', ...
  'UserData','I', ...
  'Position',[0.6224     0.89686    0.0229    0.030628+dy], ...
  'Tag','PushPGL');

h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_profile'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat plotq', ...
  'Position',[0.8725 0.89686 0.045861 0.030628+dy], ...
  'Tag','Pushbutton3');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_pu'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat reactivepower', ...
  'UserData','pu', ...
  'Position',[0.8235     0.89686    0.045861    0.030628+dy], ...
  'Tag','PushQ');
h1 = uicontrol('Parent',h0, ...
  'Units', 'normalized', ...
  'CData', fm_mat('stat_pqi'), ...
  'BackgroundColor',Theme.color02, ...
  'Callback','fm_stat qgl', ...
  'UserData','I', ...
  'Position',[0.7975     0.89686    0.0229    0.030628+dy], ...
  'Tag','PushQGL');


set(h0,'UserData',[hB; hV; hA; hP; hQ]);

if nargout > 0, fig = h0; end

%==============================================================
function stringa = setvar(input)
global Bus
busn = min(150,Bus.n);
[buss, ordbus] = sortbus_bus(Bus,busn);
hdl = findobj(gcf,'Tag','PushSort');
if ~isempty(hdl)
  if strcmp(get(hdl,'UserData'),'1n')
    [a,ordbus] = sort(getidx_bus(Bus,0));
  end
end
stringa = cell(busn,1);
for i = 1:busn,
  stringa{i,1} = fvar(input(ordbus(i)),10);
end

%==============================================================
function stringa = setx
global DAE Varname
daen = min(100,DAE.n);
stringa = cell(daen,1);
for i = 1:daen,
  stringa{i,1} = [fvar(Varname.uvars{i,1},19), fvar(DAE.x(i),10)];
end

%==============================================================
function stringa = setbus
global Bus
[stringa, ord] = sortbus_bus(Bus,150);
stringa = fm_strjoin('[',int2str(getidx_bus(Bus,ord)),']-',stringa);
hdl = findobj(gcf,'Tag','PushSort');
if ~isempty(hdl)
  if strcmp(get(hdl,'UserData'),'1n')
    [a,ord] = sort(getidx_bus(Bus,ord));
    stringa = stringa(ord);
  end
end

%==============================================================
function value = onoff(string)
switch string
case 'on'
  value = 1;
otherwise
  value = 0;
end

%==============================================================
function setyvars(h1,type)

global DAE Bus Varname

if DAE.m > 2*Bus.n
  idx = [2*Bus.n+1:DAE.m];
  if type
    string = get(h1,'String');
    if ~iscell(string)
      string = cellstr(a);
    end
  else
    string = cell(0,0);
  end
  string = [string; fm_strjoin(Varname.uvars(DAE.n+idx),' = ',num2str(DAE.y(idx)))];
  set(h1,'String',string);
end
